<template>
    <view class="container">
        <!-- 页面标题 -->
        <view class="header">
            <view class="header-title">已报名的活动</view>
            <view class="header-subtitle">查看您参与的所有活动</view>
        </view>

        <!-- 加载状态 -->
        <view v-if="loading" class="loading-container">
            <view class="loading-text">加载中...</view>
        </view>

        <!-- 空状态 -->
        <view v-else-if="activityList.length === 0" class="empty-container">
            <view class="empty-icon">📅</view>
            <view class="empty-text">暂无已报名的活动</view>
            <view class="empty-subtitle">快去报名参加活动吧！</view>
        </view>

        <!-- 活动列表 -->
        <view v-else class="activity-list">
            <view 
                v-for="(activity, index) in activityList" 
                :key="activity.id" 
                class="activity-card"
                @click="goToActivityDetail(activity.id)"
            >
                <image :src="activity.cover" mode="aspectFill" class="activity-image"></image>
                <view class="activity-content">
                    <view class="activity-title">{{ activity.location }}</view>
                    <view class="activity-description" v-html="activity.description"></view>
                    <view class="activity-info">
                        <view class="info-item">
                            <text class="info-icon">🕐</text>
                            <text class="info-text">{{ formatDateTime(activity.startTime) }} - {{ formatDateTime(activity.expireTime) }}</text>
                        </view>
                        <view class="info-item">
                            <text class="info-icon">📍</text>
                            <text class="info-text">{{ activity.location }}</text>
                        </view>
                        <view class="info-item">
                            <text class="info-icon">👥</text>
                            <text class="info-text">{{ activity.recruitedNum || 0 }}/{{ activity.recruitNum || 0 }}人</text>
                        </view>
                    </view>
                    <view class="activity-status">
                        <text class="status-text" :class="getStatusClass(activity.registerStatus) || 'status-default'">
                            {{ getStatusText(activity.registerStatus) }}
                        </text>
                    </view>
                </view>
            </view>
        </view>

        <!-- 底部提示 -->
        <view class="footer-tip">
            <text>点击活动可查看详情</text>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            loading: true,
            activityList: [],
            userId: null
        };
    },
    onLoad() {
        this.initPage();
    },
    onShow() {
        // 每次显示页面时刷新数据
        if (this.userId) {
            this.loadUserActivities();
        }
    },
    methods: {
        // 初始化页面
        initPage() {
            // 检查用户登录状态
            const userData = this.f.storage('user_data');
            if (this.f.isnull(userData) === '') {
                this.f.msg('请先登录');
                this.f.nav('/pages/login/login1');
                return;
            }
            
            this.userId = userData.userId;
            this.loadUserActivities();
        },

        // 加载用户已报名的活动
        loadUserActivities() {
            this.loading = true;
            
            const params = {
                pageNo: 1,
                pageSize: 20
            };
            
            // 使用与ActivityList相同的接口方式
            this.$api.req('/station/activity/page', params, 'GET', 'JSON', false)
                .then(res => {
                    this.loading = false;
                    
                    if (res.code === 0) {
                        const newList = res.data.list || [];
                        
                        // 模拟已报名的活动（实际项目中应该从后端获取用户的报名记录）
                        // 这里暂时显示所有活动，实际应该根据用户的报名状态筛选
                        this.activityList = newList.map(activity => {
                            // 模拟报名状态：随机分配状态
                            const statuses = [1, 2, 3]; // 1-待审核, 2-已通过, 3-已拒绝
                            const randomStatus = statuses[Math.floor(Math.random() * statuses.length)];
                            
                            return {
                                ...activity,
                                registerStatus: randomStatus,
                                registerStatusText: this.getStatusText(randomStatus)
                            };
                        });
                        
                        // 格式化时间显示
                        this.activityList.forEach(activity => {
                            activity.startTimeText = this.formatDateTime(activity.startTime);
                            activity.expireTimeText = this.formatDateTime(activity.expireTime);
                        });
                    } else {
                        this.f.msg(res.msg || '获取活动列表失败');
                        this.activityList = [];
                    }
                })
                .catch(err => {
                    this.loading = false;
                    console.error('获取活动列表失败:', err);
                    this.f.msg('网络错误，请稍后重试');
                    this.activityList = [];
                });
        },

        // 跳转到活动详情
        goToActivityDetail(activityId) {
            this.f.nav(`/pages/ActivityDetail/ActivityDetail?id=${activityId}`);
        },

        // 获取状态样式类
        getStatusClass(status) {
            if (status === null || status === undefined) {
                return 'status-default';
            }
            const classMap = {
                0: 'status-default',
                1: 'status-warning',
                2: 'status-success',
                3: 'status-error'
            };
            return classMap[status] || 'status-default';
        },

        // 获取状态文本
        getStatusText(status) {
            if (status === null || status === undefined) {
                return '未报名';
            }
            const statusMap = {
                0: '未报名',
                1: '待审核',
                2: '已通过',
                3: '已拒绝'
            };
            return statusMap[status] || '未报名';
        },

        // 格式化日期时间
        formatDateTime(timestamp) {
            if (!timestamp) return '';
            
            const date = new Date(timestamp);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        },

        // 去除 HTML 标签
        stripHtmlTags(html) {
            if (!html) return '';
            return html.replace(/<[^>]*>/g, '').trim();
        }
    }
}
</script>

<style scoped>
.container {
    background-color: #f5f5f5;
    min-height: 100vh;
    padding: 20rpx;
}

/* 页面标题 */
.header {
    text-align: center;
    padding: 40rpx 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    margin: -20rpx -20rpx 30rpx -20rpx;
    color: white;
}

.header-title {
    font-size: 36rpx;
    font-weight: bold;
    margin-bottom: 10rpx;
}

.header-subtitle {
    font-size: 28rpx;
    opacity: 0.9;
}

/* 加载状态 */
.loading-container {
    text-align: center;
    padding: 100rpx 0;
}

.loading-text {
    font-size: 28rpx;
    color: #999;
}

/* 空状态 */
.empty-container {
    text-align: center;
    padding: 100rpx 0;
}

.empty-icon {
    font-size: 120rpx;
    margin-bottom: 30rpx;
}

.empty-text {
    font-size: 32rpx;
    color: #333;
    margin-bottom: 10rpx;
}

.empty-subtitle {
    font-size: 26rpx;
    color: #999;
}

/* 活动列表 */
.activity-list {
    padding: 20rpx;
}

.activity-card {
    background: white;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    overflow: hidden;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
    transition: all 0.3s ease;
    position: relative;
}

.activity-card:active {
    transform: translateY(2rpx);
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.12);
}

.activity-image {
    width: 100%;
    height: 320rpx;
    object-fit: cover;
}

.activity-content {
    padding: 24rpx;
}

.activity-title {
    font-size: 36rpx;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 12rpx;
    line-height: 1.3;
}

.activity-description {
    font-size: 28rpx;
    color: #7f8c8d;
    line-height: 1.6;
    margin-bottom: 20rpx;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.activity-info {
    display: flex;
    flex-direction: column;
    gap: 12rpx;
    margin-bottom: 20rpx;
    padding: 16rpx;
    background: #f8f9fa;
    border-radius: 12rpx;
}

.info-item {
    display: flex;
    align-items: center;
    gap: 12rpx;
}

.info-icon {
    font-size: 28rpx;
    margin-right: 8rpx;
}

.info-text {
    font-size: 26rpx;
    color: #495057;
    font-weight: 500;
}

.activity-status {
    display: flex;
    justify-content: flex-end;
    margin-top: 8rpx;
}

.status-text {
    padding: 12rpx 20rpx;
    border-radius: 25rpx;
    font-size: 24rpx;
    font-weight: 600;
    transition: all 0.2s ease;
}

.status-default {
    background: linear-gradient(135deg, #e9ecef, #f8f9fa);
    color: #495057;
    border: 1rpx solid #dee2e6;
}

.status-warning {
    background: linear-gradient(135deg, #fff3cd, #ffeaa7);
    color: #856404;
    border: 1rpx solid #ffd43b;
}

.status-success {
    background: linear-gradient(135deg, #d4edda, #a8e6cf);
    color: #155724;
    border: 1rpx solid #28a745;
}

.status-error {
    background: linear-gradient(135deg, #f8d7da, #ffb3ba);
    color: #721c24;
    border: 1rpx solid #dc3545;
}

/* 底部提示 */
.footer-tip {
    text-align: center;
    padding: 30rpx 0;
    color: #999;
    font-size: 24rpx;
}
</style>
